Repository navigation
factory-server: record the cost OpenRouter charged - #294
Merged
Merged
Conversation
After an OpenRouter run the server reads each generation's total_cost from GET /api/v1/generation with the run's key and adds the sum through Store::add_cost, not marked estimated. The lookup runs in a background task with retries for about a minute, so it never fails the run or delays the job's next step. When an id stays unreadable or the output showed none, the run adds no cost and the log says so with the token counts. A fetched $0 shows $0.00. Co-authored-by: Copilot <[email protected]>
A zero from a subscription run stays a dash unless OpenRouter confirmed it, which the job_costs row now records in `fetched`. A late lookup files its charge under the stage and pull request of the run, and the timeline no longer reads that entry as a step change. A terminal job keeps polling while a lookup is pending. The lookup has a 90 second wall-clock deadline and records nothing when it expires. Comments that repeated the code are removed. Co-authored-by: Copilot <[email protected]>
The job page reads the pending lookup before the log and the cost, so a lookup that ends mid-read keeps the page polling. A run's charge is filed under the job as it was when the run was dispatched, so a cancellation before the report cannot move it off the step. The design and the Local-only rule in the store now say a confirmed OpenRouter zero shows as $0.00, and the project forge migration comment is back above its function. Co-authored-by: Copilot <[email protected]>
The worker receives the job that selected the task instead of reading it again, so a cancellation after dispatch cannot move a charge off its step, and a store error is no longer dropped. The job page reads the job row before the pending lookup, which closes the race where a lookup began and the job ended between the two reads. Co-authored-by: Copilot <[email protected]>
A cancel can end the job before the worker reaches its cost lookup, so the page rendered by the cancel redirect had no poller and missed the late charge. The scheduler now counts the worker in cost_lookups from dispatch until it ends, and the lookup it starts counts on its own. Co-authored-by: Copilot <[email protected]>
A cancel while the agent request was built left a terminal page with no poller, and the scheduler then started the worker from a stale snapshot. The registration now checks the job's stage under the store lock and starts no worker when it changed. Co-authored-by: Copilot <[email protected]>
A store error while registering the lookup started the worker with no pending marker. The scheduler now logs it and retries on the next pass. A dashboard test loads the cancelled job page after registration and checks it polls until the charge shows in the fragment. Co-authored-by: Copilot <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
An OpenRouter run bills real money, so the job total should show what OpenRouter charged. After a run, the server reads each generation's
total_costfromGET /api/v1/generation?id=with the run's key and records the sum. The lookup runs in a background task and never delays or fails the run. When a cost cannot be read, the run adds nothing.Details:
$0.00only when OpenRouter confirmed it (job_costs.fetched).Docs
docs/design.md: the Job cost row and the sections on scheduling and recording an OpenRouter run describe thefetchedflag, the step attribution, the worker registration, polling while a lookup is pending and the deadline.Verification
nix develop -c cargo test -p factory-server --features e2e-fakes: passed, including a dashboard test that loads a cancelled job page after registration, checks it polls, and checks the fragment shows the charge.nix develop -c cargo clippy --workspace --all-targets --features e2e-fakes: no warnings.nix develop -c just e2e: 238 passed on the previous commit; this change touches no browser code.